<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>中因医院</title>
    <meta http-equiv="Access-Control-Allow-Origin" content="*" />
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--搜索框-->
<div class="searchBar">
    名称：
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off" />
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<!--表格主体-->
<table id="menu" lay-filter="menu"></table>
<!--工具栏 toolbar-->
<div id="toolbar" style="display: none;">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</div>
<!--操作栏 tool-->
<script type="text/html" id="operateBar">
    <a class="layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除
    </a>
</script>
<!-- 模态框 -->
<div id="modal" style="display:none;">
    <form class="layui-form" lay-filter="entity" action="">
        <input type="text" name="menuId" id="menuId" class="layui-input"
               style="display: none;" />
        <div class="layui-form-item">
            <label class="layui-form-label">上级目录</label>
            <div class="layui-input-inline">
                <select name="parentId" id="parentId">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单类型</label>
            <div class="layui-input-inline">
                <input type="radio" name="menuType" value="M" title="目录"><div
                    class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"> </i>
                <div>目录</div></div>
                <input type="radio" name="menuType" value="C" title="菜单"
                       checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i
                    class="layui-anim layui-icon"> </i><div>菜单</div></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-inline">
                <input type="text" name="menuName" required="" lay-verify="required" placeholder="请输入菜单名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">显示顺序</label>
            <div class="layui-input-inline">
                <input type="text" name="orderNum" required="" lay-verify="required" placeholder="请输入显示顺序" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">请求地址</label>
            <div class="layui-input-inline">
                <input type="text" name="url" required="" lay-verify="required"
                       placeholder="请输入请求地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限标识</label>
            <div class="layui-input-inline">
                <input type="text" name="perms" required="" lay-verify="required" placeholder="请输入权限标识" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单状态</label>
            <div class="layui-input-inline">
                <input type="radio" name="visible" value="0" title="显示"
                       checked=""><div class="layui-unselect layui-form-radio"><i class="layui-anim
layui-icon"> </i><div>显示</div></div>
                <input type="radio" name="visible" value="1" title="隐藏"><div
                    class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim
layui-icon"> </i><div>隐藏</div></div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="menu">立即提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary">重置
                </button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table','layer', 'form', 'jquery'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        table.render({
            elem: '#menu'
            ,height: 525
            ,id:'testReload'
            ,url: '/api/menu/page' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'menuId', title: 'ID', sort: true, fixed: 'left'}
                ,{field: 'menuName', title: '菜单名称'}
                ,{field: 'orderNum', title: '显示顺序'}
                ,{field: 'url', title: '请求地址'}
                ,{field: 'perms', title: '权限标识'}
                ,{field: 'createTime', title: '创建时间', sort: true, templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                ,{field: 'remark', title: '备注'}
                ,{title: '操作', toolbar:'#operateBar', align:'center'}
            ]]
            ,toolbar: '#toolbar'
            ,response : {
                statusCode : 1
            }
            ,parseData : function (res) {
                if (res.status == 1) {
                    return {
                        'code' : res.status,
                        'msg' : res.message,
                        'count' : res.total,
                        'data' : res.data
                    }
                }
            }
        });
        $.get('/api/menu', function (res) {
            $('#parentId').empty(); // 清空下拉框
            $('#parentId').append(new Option("请选择", 0));
            $.each(res.data, function (index, obj) {
                if (obj.parentId == 0) {
                    $('#parentId').append(new Option(obj.menuName, obj.menuId));
                }
            });
            form.render();
        });
        table.on('toolbar(menu)', function (obj) {
            switch (obj.event) {
                case 'add':
                    open2("新增菜单", null);
                    break;
            }
        });
        //删除
        table.on('tool(menu)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'delete':
                    layer.confirm('确定要删除吗？', function (index) {
// 调用接口
// 在回调方法中，将删除项从DOM中删除
                        $.ajax({
                            url : '/api/menu/'+data.menuId,
                            type : 'delete',
                            dataType: 'json',
                            success : function (res) {
                                if (res.status == 1) {
                                    layer.msg(res.message);
                                    obj.del();
                                } else {
                                    layer.msg(res.message);
                                }
                            }
                        });
// 关闭确认对话框
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    open2('修改页面', data);
                    break;
            }
        });
        function open2(title, entity) {
            form.val('entity', entity);
            layer.open({
                type : 1
                ,title : title
                ,content: $('#modal')
                ,offset: '10px'
                ,area:['500px', '95%']
                ,end:function () {
                    table.reload('testReload');
                }
            });
        }
        form.on('submit(menu)', function () {
// 获取表单中的数据
            var data = form.val('entity');
            var type = data.menuId ? 'put':'post'
// 调用接口，发送获取到的数据
            $.ajax({
                url:'/api/menu',
                type:type,
                dataType:'json',
                contentType:'application/json',
                data: JSON.stringify(data),
                success: function (res) {
                    if (res.status == 1){
                        layer.msg(res.message, {time:1*1000}, function () {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
            return false;
        });
        //搜索查询功能
        var active = {
            reload: function () {
                var menuName = $('#name');
                table.reload('testReload',{
                    page : {
                        curr : 1
                    }
                    ,where: {
                        'menuName':menuName.val()
                    }
                });
            }
        };
        $('.searchBar .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        })
    });
</script>
</body>
</html>